package com.ewei.web.crawler.utils;

import org.bson.Document;

import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.ServerAddress;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;

public class MongoDataSource {
	private static MongoClient session = null;
	static{
		MongoClientOptions.Builder build = new MongoClientOptions.Builder();
		build.connectionsPerHost(Environment.MAX_CONNECTION);
		build.threadsAllowedToBlockForConnectionMultiplier(Environment.MAX_THREAD_BLOCK);
		build.connectTimeout(Environment.MAX_TIMEOUT);
		build.maxWaitTime(Environment.MAX_WAIT_TIME);
		MongoClientOptions options = build.build();
		session = new MongoClient(
				new ServerAddress(Environment.DEFAULT_HOST,Environment.DEFAULT_PORT),options);
	}
	private static MongoDatabase getDatabase(String dbName){
		return session.getDatabase(dbName);
	}
	public static MongoCollection<Document> getCollection(String collectionName){
		return getDatabase(Environment.DATABASE_NAME).getCollection(collectionName);
	}
}
